using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace sales.controls
{
    public partial class advTreeView : UserControl
    {
        public advTreeView()
        {
            InitializeComponent();
        }

        public bool checkSubNodeIfParentCheched = true;
        public delegate void DoSelect();
        private DoSelect DoSelectFunc = null;
        public DoSelect SelectButtonAction
        {
            set { DoSelectFunc = value; }
        }
        public string SelectButtonText
        {
            set { selectBtn.Text = value; }
        }

        public delegate void DoExpandView(bool bigMode);
        private DoExpandView expandViewFunc = null;
        public DoExpandView WhenDoExpandView
        {
            set { expandViewFunc = value; }
        }
        
        public  TreeView myTreeView
        {
            get { return treeView;}
        }
        public void SetTreeSize(Size sz)
        {
            this.Size = sz;
            treeViewPnl.Size = sz;
            treeView.Height = treeViewPnl.Height - closeTreeBtn.Height - 5;
            treeView.Width = treeViewPnl.Width - 10;

            treeBigBtn.Location = new Point(3, treeViewPnl.Height - treeBigBtn.Height - 3);
            treeSmallBtn.Location = new Point(treeBigBtn.Location.X + treeBigBtn.Width, treeBigBtn.Location.Y);

            selectAllBtn.Location = new Point(treeSmallBtn.Location.X + treeSmallBtn.Width + 10, treeSmallBtn.Location.Y);
            unselectAllBtn.Location = new Point(selectAllBtn.Location.X + selectAllBtn.Width, selectAllBtn.Location.Y);

            expandAllBtn.Location = new Point(unselectAllBtn.Location.X + unselectAllBtn.Width + 10, unselectAllBtn.Location.Y);
            collapseAllBtn.Location = new Point(expandAllBtn.Location.X + expandAllBtn.Width, expandAllBtn.Location.Y);

            closeTreeBtn.Location = new Point(treeViewPnl.Width - closeTreeBtn.Width - 30, treeViewPnl.Height - closeTreeBtn.Height - 3);
            selectBtn.Location = new Point(closeTreeBtn.Location.X - selectBtn.Width - 3, closeTreeBtn.Location.Y);
        }

        private void closeTreeBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void selectBtn_Click(object sender, EventArgs e)
        {
            if (DoSelectFunc == null) return;
            DoSelectFunc();
        }

        private void expandAllBtn_Click(object sender, EventArgs e)
        {
            treeView.ExpandAll();
        }

        private void collapseAllBtn_Click(object sender, EventArgs e)
        {
            treeView.CollapseAll();
        }

        private void treeBigBtn_Click(object sender, EventArgs e)
        {
            if (expandViewFunc == null) return;
            expandViewFunc(true);
        }

        private void treeSmallBtn_Click(object sender, EventArgs e)
        {
            if (expandViewFunc == null) return;
            expandViewFunc(false);
        }

        private void treeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (checkSubNodeIfParentCheched) prodTemplateLibs.CheckAllSubNodes(e.Node, e.Node.Checked);
        }

        private void selectAllBtn_Click(object sender, EventArgs e)
        {
            for (int idx = 0; idx < treeView.Nodes.Count; idx++) prodTemplateLibs.CheckAllSubNodes(treeView.Nodes[idx], true);
        }

        private void unselectAllBtn_Click(object sender, EventArgs e)
        {
            for (int idx = 0; idx < treeView.Nodes.Count; idx++) prodTemplateLibs.CheckAllSubNodes(treeView.Nodes[idx], false);
        }
    }
}
